/*下拉框选项的样式显示*/
const getClass = (issue: number) => {
  if (issue === 2) {
    return 'show-issue';
  }
  return 'no-issue';
};

const getFillDesc = (row: any, dict: any, key: any) => {
  return row[dict + '_' + key + 'Desc'] || '无';
};

// 获取旧值（用于 tooltip 显示）
const getOldValue = (row: any, key: any) => {
  return row[key + '_old'] || '无变化';
};

// 计算高亮样式
const highlightClass = (row: any, key: any) => {
  return row.highlight && row.highlight[key] ? 'highlight-cell' : '';
};

const hasFillDesc = (row: any, dict: any, key: any) => {
  return getFillDesc(row, dict, key) !== '无';
};

// 格式化零值函数
const formatZeroValue = (value: any) => {
  // 处理数字和字符串类型的零值
  if (value === 0 || value === '0' || value === '0.00' || value === 0.0) {
    return '';
  }
  // 如果 value 是字符串且包含 %，直接返回
  if (typeof value === 'string' && value.includes('%')) {
    return value;
  }
  return value;
};

// 格式化百分比函数
const formatPercentageValue = (value: any) => {
  if (value === null || value === undefined) {
  return '';
}
  // 如果 value 是字符串且包含 %，直接返回
  if (typeof value === 'string' && value.includes('%')) {
    return value;
  }

  return (value * 100).toFixed(2) + '%';
};

/**
 * 计算除了百分比科目的所有科目总计
 * @param tableList
 */
const getTotal = (tableList: any[]): number => {
  if (!tableList || tableList.length === 0) return 0;

  // 只计算isPercentage=false 的数据行
  return tableList
    .filter((item) => !item.isPercentage)
    .reduce((sum, item) => {
      const value = Number(item.total) || 0;
      return sum + value;
    }, 0)
    .toFixed(2);
};
/**
 * 获取需要参与计算的列（排除备注列）
 * @param yardTypeList 院区类型字典列表
 * @param titleList 标题列表
 * @returns 计算列的数组
 */
const getSumColumns = (yardTypeList: any[], titleList: any[]): string[] => {
  const columns: string[] = [];

  // 添加动态列（不含Desc后缀的列）
  titleList.forEach((item) => {
    yardTypeList.forEach((dict) => {
      const prop = `${dict.value}_${item.key}`;
      columns.push(prop);
    });
  });

  // 添加合计列
  yardTypeList.forEach((dict) => {
    columns.push(`${dict.value}_total`);
  });

  // 添加总合计列
  columns.push('total');

  return columns;
};

/**
 * 自定义表格合计方法
 * @param param 表格summary方法的参数
 * @param sumColumns 需要计算的列集合
 * @param dataFilterField 数据过滤字段名（默认为'isPercentage'）
 * @returns 合计行数据数组
 */
const getSummaries = (param: { columns: any[]; data: any[] }, sumColumns: string[], dataFilterField: string = 'isPercentage'): any[] => {
  const { columns, data } = param;
  const sums: any[] = [];
  const validData = data.filter((item) => !item[dataFilterField]);

  columns.forEach((column, index) => {
    if (index === 0) {
      sums[index] = '合计';
      return;
    }

    const property = column.property;
    if (!property || !sumColumns.includes(property)) {
      sums[index] = '';
      return;
    }

    // 只计算数值列
    const values = validData.map((item) => {
      const val = item[property];
      return val ? parseFloat(val) : 0;
    });

    if (values.length > 0 && !values.every((value) => isNaN(value))) {
      const sum = values.reduce((prev, curr) => prev + curr, 0);
      sums[index] = Number.isInteger(sum) ? sum.toString() : sum.toFixed(2);
    } else {
      sums[index] = '';
    }
  });

  return sums;
};


/**
 * 自定义高度
 * @param showSearch
 * @param options
 */
const useTableHeight = (
  showSearch: Ref<boolean>,
  options: { withSearch: string; withoutSearch: string } = {
    withSearch: '68vh',
    withoutSearch: '78vh'
  }
) => {
  return computed(() => ({
    height: showSearch.value ? options.withSearch : options.withoutSearch
  }));
};

export { getTotal, getClass, getOldValue, highlightClass, getFillDesc, hasFillDesc, formatZeroValue,formatPercentageValue, getSumColumns, getSummaries, useTableHeight };
